_Pragma("once");

#include <ctime>
#include "EditablePage.h"
#include "DateTimeUtility.h"
#include "StringUtility.h"
#include "MultiPage.h"
#include "StringListEditor.h"


/**
 * 第八页 显示上下一条离线指令页面
 * @author lizb
 */
public class NextOfflineCommandPage : public MultiPage<EditablePage>{
    private static Logger log = LoggerFactory.getLogger(NextOfflineCommandPage.class);

    private static final int MAX_INNER_PAGE_NUM = 100;
    private static final String INVALID_YMD = "0000_00_00";
    private static final String INVALID_HMS = "00:00:00";
    private static final String TIME_FORMAT = "yyyy_MM_dd HH:mm:ss";

    private MultiPage multiPage = new FixedMultiPage(MAX_INNER_PAGE_NUM,this,getPageNum());

    public NextOfflineCommandPage(int pageNum, long mRefreshPeriod) {
        super(pageNum, mRefreshPeriod);
    }

    @Override
    public void refreshPage() {
        try {
            OfflineCommandBean offlineCommandByTime = OfflineCommandManager.readNextOfflineCommand(new Date(), getMultiPageNum());
            LedScreen.getInstance().setText(getPageContent(offlineCommandByTime, getPageNum()));
        } catch (IOException | ParseException e) {
            log.debug(LoggerUtil.convertException2String(e));
        }
    }
    protected List<ScreenLine> getPageContent(OfflineCommandBean offlineCommandBean, int pageNum) {
        String ymd;
        String hms;
        String offlineCommand;
        if (offlineCommandBean == null) {
            ymd = INVALID_YMD;
            hms = INVALID_HMS;
            offlineCommand = LoopManager.getInstance().getAllClosedCommand();
        } else {
            SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT);
            Date date = offlineCommandBean.getDate();
            String formatDate = format.format(date);
            String[] split = formatDate.split(" ");
            ymd = split[0];
            hms = split[1];
            log.debug(offlineCommandBean.getCommand());
            offlineCommand =StringUtil.formatCommand(offlineCommandBean.getCommand(),LoopManager.getInstance().getLoopNum());
        }
        List<ScreenLine> result = new ArrayList<>();
        result.add(new ScreenLine.ScreenLineBuilder().text(ymd).pageNum(pageNum).build());
        result.add(new ScreenLine.ScreenLineBuilder().text(hms).build());
        if (offlineCommand == null) {
            result.add(new ScreenLine.ScreenLineBuilder().text("").build());
            result.add(new ScreenLine.ScreenLineBuilder().text("").build());
            return result;
        }
        if(offlineCommand.length()>LedScreen.SCREEN_WIDTH) {
            result.add(new ScreenLine.ScreenLineBuilder().text(offlineCommand.substring(0, LedScreen.SCREEN_WIDTH)).build());
            //todo  不需要截取长度啊 在builder里面有啊
            result.add(new ScreenLine.ScreenLineBuilder().text(offlineCommand.substring(LedScreen.SCREEN_WIDTH,Math.min(LedScreen.SCREEN_WIDTH*2,offlineCommand.length()))).build());
        } else {
            result.add(new ScreenLine.ScreenLineBuilder().text(offlineCommand).build());
            result.add(new ScreenLine.ScreenLineBuilder().text("D").alignRight().build());
        }
        return result;
    }

    @Override
    public void onKeySet() {
        super.onKeySet();
    }

    @Override
    public void saveVal() {
        if (OfflineCommandManager.deleteAllOfflineCommand()) {
            JobManager.getInstance().cancelOfflineCommandJob();
            new Thread(()-> OfflineCommandManager.requestForOfflineCommand(Connection.getInstance())).start();
        }
    }

    @Override
    public void initEditUnits() {
        const StringVectorPtr deleteLabelPtr{new StringVectorPtr::element_type{"delete"}};

        addEditUnit(new StringListEditor(4, 15, 15, deleteLabelPtr));
    }

}
